package com.xplusplus.security.controller;

import com.xplusplus.security.domain.Result;
import com.xplusplus.security.domain.TemporaryProjectHeader;
import com.xplusplus.security.service.TemporaryProjectHeaderService;
import com.xplusplus.security.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * @Author: zhouweixin
 * @Description: 临时项目表头
 * @Date: Created in 11:17 2018/9/4
 * @Modified By:
 */
@RestController
@RequestMapping(value = "/temporaryProjectHeader")
@Api(tags = "临时项目表头接口")
public class TemporaryProjectHeaderController {

    @Autowired
    private TemporaryProjectHeaderService temporaryProjectHeaderService;

    @ApiOperation(value = "新增")
    @PostMapping(value = "/add")
    public Result<TemporaryProjectHeader> add(
            @ApiParam(value = "临时项目表头") @RequestBody TemporaryProjectHeader header) {
        return ResultUtil.success(temporaryProjectHeaderService.add(header));
    }

    @ApiOperation(value = "更新")
    @PostMapping(value = "/update")
    public Result<TemporaryProjectHeader> update(
            @ApiParam(value = "临时项目表头") @RequestBody TemporaryProjectHeader header) {
        return ResultUtil.success(temporaryProjectHeaderService.update(header));
    }

    @ApiOperation(value = "通过主键查询")
    @GetMapping(value = "/getById")
    public Result<TemporaryProjectHeader> getById(
            @ApiParam(value = "主键") @RequestParam Long id) {
        return ResultUtil.success(temporaryProjectHeaderService.getById(id));
    }

    @ApiOperation(value = "通过主键和申请人删除")
    @PostMapping(value = "/deleteByIdAndApplicant")
    public Result<TemporaryProjectHeader> deleteByIdAndApplicant(
            @ApiParam(value = "主键数组") @RequestParam Long[] ids,
            @ApiParam(value = "申请人") @RequestParam String applicantId) {
        temporaryProjectHeaderService.deleteByIdAndApplicant(ids, applicantId);
        return ResultUtil.success();
    }

    @ApiOperation(value = "通过主键审核")
    @PostMapping(value = "/auditById")
    public Result<Object> auditById(
            @ApiParam(value = "主键") @RequestParam Long id,
            @ApiParam(value = "审核人主键") @RequestParam String auditorId,
            @ApiParam(value = "审核状态: 1同意；2不同意", defaultValue = "1") @RequestParam(defaultValue = "1") Integer status,
            @ApiParam(value = "审核意见", defaultValue = "") @RequestParam(defaultValue = "") String note) {
        temporaryProjectHeaderService.auditById(id, auditorId, status, note);
        return ResultUtil.success();
    }

    @ApiOperation(value = "查询所有-分页")
    @GetMapping(value = "/getAllByPage")
    public Result<Page<TemporaryProjectHeader>> getAllByPage(
            @ApiParam(value = "页码(默认为0)") @RequestParam(value = "page", defaultValue = "0") Integer page,
            @ApiParam(value = "每页记录数(默认为10)") @RequestParam(value = "size", defaultValue = "10") Integer size,
            @ApiParam(value = "排序字段名(默认为id)") @RequestParam(value = "sortFieldName", defaultValue = "id") String sortFieldName,
            @ApiParam(value = "排序方向(0:降序；1升序；默认为1)") @RequestParam(value = "asc", defaultValue = "1") Integer asc) {
        return ResultUtil.success(temporaryProjectHeaderService.getAllByPage(page, size, sortFieldName, asc));
    }

    @ApiOperation(value = "通过申请人,状态和日期段查询-分页")
    @GetMapping(value = "/getByApplicantAndStatusAndDateByPage")
    public Result<Page<TemporaryProjectHeader>> getByApplicantAndDateByPage(
            @ApiParam(value = "开始日期, 格式为yyyy-MM-dd, 2000-01-01表示忽略该条件", defaultValue = "2000-01-01") @RequestParam(defaultValue = "2000-01-01") @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
            @ApiParam(value = "结束日期, 格式为yyyy-MM-dd, 2000-01-01表示忽略该条件", defaultValue = "2000-01-01") @RequestParam(defaultValue = "2000-01-01") @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate,
            @ApiParam(value = "申请人主键, 默认为-1", defaultValue = "-1") @RequestParam(defaultValue = "-1") String applicantId,
            @ApiParam(value = "状态(默认为-1)", defaultValue = "-1") @RequestParam(defaultValue = "-1") Integer status,
            @ApiParam(value = "页码(默认为0)", defaultValue = "0") @RequestParam(value = "page", defaultValue = "0") Integer page,
            @ApiParam(value = "每页记录数(默认为10)", defaultValue = "10") @RequestParam(value = "size", defaultValue = "10") Integer size,
            @ApiParam(value = "排序字段名(默认为id)", defaultValue = "id") @RequestParam(value = "sortFieldName", defaultValue = "id") String sortFieldName,
            @ApiParam(value = "排序方向(0:降序；1升序；默认为1)", defaultValue = "1") @RequestParam(value = "asc", defaultValue = "1") Integer asc) {
        return ResultUtil.success(temporaryProjectHeaderService.getByApplicantAndStatusAndDateByPage(startDate, endDate, applicantId, status, page, size, sortFieldName, asc));
    }

    @ApiOperation(value = "通过经办人,状态和日期段查询-分页")
    @GetMapping(value = "/getByOperatorAndStatusAndDateByPage")
    public Result<Page<TemporaryProjectHeader>> getByOperatorAndDateByPage(
            @ApiParam(value = "开始日期, 格式为yyyy-MM-dd, 2000-01-01表示忽略该条件", defaultValue = "2000-01-01") @RequestParam(defaultValue = "2000-01-01") @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
            @ApiParam(value = "结束日期, 格式为yyyy-MM-dd, 2000-01-01表示忽略该条件", defaultValue = "2000-01-01") @RequestParam(defaultValue = "2000-01-01") @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate,
            @ApiParam(value = "申请人主键, 默认为-1", defaultValue = "-1") @RequestParam(defaultValue = "-1") String operatorId,
            @ApiParam(value = "状态(默认为-1)", defaultValue = "-1") @RequestParam(defaultValue = "-1") Integer status,
            @ApiParam(value = "页码(默认为0)", defaultValue = "0") @RequestParam(value = "page", defaultValue = "0") Integer page,
            @ApiParam(value = "每页记录数(默认为10)", defaultValue = "10") @RequestParam(value = "size", defaultValue = "10") Integer size,
            @ApiParam(value = "排序字段名(默认为id)", defaultValue = "id") @RequestParam(value = "sortFieldName", defaultValue = "id") String sortFieldName,
            @ApiParam(value = "排序方向(0:降序；1升序；默认为1)", defaultValue = "1") @RequestParam(value = "asc", defaultValue = "1") Integer asc) {
        return ResultUtil.success(temporaryProjectHeaderService.getByOperatorAndStatusAndDateByPage(startDate, endDate, operatorId, status, page, size, sortFieldName, asc));
    }

    @ApiOperation(value = "通过审核人,状态和日期段查询-分页")
    @GetMapping(value = "/getByAuditorAndStatusAndDateByPage")
    public Result<Page<TemporaryProjectHeader>> getByAuditorAndDateByPage(
            @ApiParam(value = "开始日期, 格式为yyyy-MM-dd, 2000-01-01表示忽略该条件", defaultValue = "2000-01-01") @RequestParam(defaultValue = "2000-01-01") @DateTimeFormat(pattern = "yyyy-MM-dd") Date startDate,
            @ApiParam(value = "结束日期, 格式为yyyy-MM-dd, 2000-01-01表示忽略该条件", defaultValue = "2000-01-01") @RequestParam(defaultValue = "2000-01-01") @DateTimeFormat(pattern = "yyyy-MM-dd") Date endDate,
            @ApiParam(value = "申请人主键, 默认为-1", defaultValue = "-1") @RequestParam(defaultValue = "-1") String auditorId,
            @ApiParam(value = "状态(默认为-1)", defaultValue = "-1") @RequestParam(defaultValue = "-1") Integer status,
            @ApiParam(value = "页码(默认为0)", defaultValue = "0") @RequestParam(value = "page", defaultValue = "0") Integer page,
            @ApiParam(value = "每页记录数(默认为10)", defaultValue = "10") @RequestParam(value = "size", defaultValue = "10") Integer size,
            @ApiParam(value = "排序字段名(默认为id)", defaultValue = "id") @RequestParam(value = "sortFieldName", defaultValue = "id") String sortFieldName,
            @ApiParam(value = "排序方向(0:降序；1升序；默认为1)", defaultValue = "1") @RequestParam(value = "asc", defaultValue = "1") Integer asc) {
        return ResultUtil.success(temporaryProjectHeaderService.getByAuditorAndStatusAndDateByPage(startDate, endDate, auditorId, status, page, size, sortFieldName, asc));
    }


}
